<?php

namespace app\common\controller;

use app\common\controller\Common;
use PhpOffice\PhpSpreadsheet\Shared\Trend\Trend;

/**
 * 对接钉钉api
 */
class DingApi
{
    protected $appkey = 'dinghywthcem47v7ebvh';
    protected $appsecret = '9AxGxTFNVSdIoiOs6czNWKtDdVYjfaq5dJXo2H8AX_4R1zNLrqIj924RoZA7phOm';
    protected $agent_id = '1356027999';

    public function __construct(){
        // halt(session('access_token'));
        if(time() > session('access_token_add_time') + 7000 || !session('access_token')){
            //获取企业内部应用的access_token，access_token的有效期为7200秒（2小时）
            $res = self::gettoken();
            if($res['errcode'] == 0){
                session('access_token', $res['access_token']);
                session('access_token_add_time', time());
            }
        }
    }

    /**
     * 获取企业内部应用的access_token
     */
    private function gettoken()
    {
        $url = "https://oapi.dingtalk.com/gettoken?appkey={$this->appkey}&appsecret={$this->appsecret}";
        $res = Common::curl($url);
        return $res;
    }

    /**
     * 获取在职员工列表
     */
    public function queryonjob($data)
    {
        $url = "https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/queryonjob?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 获取部门列表
     */
    public function listsub($data = [])
    {
        $url = "https://oapi.dingtalk.com/topapi/v2/department/listsub?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 获取子部门ID列表
     */
    public function listsubid($data)
    {
        $url = "https://oapi.dingtalk.com/topapi/v2/department/listsubid?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 获取部门用户基础信息
     */
    public function listsimple($data)
    {
        $url = "https://oapi.dingtalk.com/topapi/user/listsimple?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 根据手机号获取userid
     */
    public function getbymobile($data)
    {
        $url = "https://oapi.dingtalk.com/topapi/v2/user/getbymobile?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 根据userid获取用户详情
     */
    public function user_get($data)
    {
        $url = "https://oapi.dingtalk.com/topapi/v2/user/get?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 获取部门详情
     */
    public function department_get($data)
    {
        $url = "https://oapi.dingtalk.com/topapi/v2/department/get?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 获取用户考勤数据
     */
    public function attendance_getupdatedata($data)
    {
        $url = "https://oapi.dingtalk.com/topapi/attendance/getupdatedata?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 获取打卡结果
     */
    public function attendance_list($data)
    {
        $url = "https://oapi.dingtalk.com/attendance/list?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 获取打卡详情
     */
    public function attendance_listRecord($data)
    {
        $url = "https://oapi.dingtalk.com/attendance/listRecord?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 发送工作通知
     */
    public function asyncsend_v2($data)
    {
        $data['agent_id'] = $this->agent_id;
        $url = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 发送工作通知
     */
    public function workrecord_add($data)
    {
        $url = "https://oapi.dingtalk.com/topapi/workrecord/add?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 更新工作通知状态栏
     */
    public function status_bar_update($data)
    {
        $data['agent_id'] = $this->agent_id;
        $url = "https://oapi.dingtalk.com/topapi/message/corpconversation/status_bar/update?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 上传媒体文件
     */
    public function media_upload($data)
    {
        $url = "https://oapi.dingtalk.com/media/upload?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }

    /**
     * 上传打卡记录
     */
    public function record_upload($data)
    {
        $url = "https://oapi.dingtalk.com/topapi/attendance/record/upload?access_token=" . session('access_token');
        $res = Common::curl($url, $data, 'post');
        return $res;
    }
}